﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Folding;

namespace CodeStash.Addin.Helpers
{
    public class AvalonEditHelper
    {
        private static Lazy<AvalonEditHelper> instance = new Lazy<AvalonEditHelper>(() => new AvalonEditHelper());

        private static Dictionary<string, string> codeStashToAvalonEditTranslateLookups =
            new Dictionary<string, string>();

        private static Dictionary<string, IHighlightingDefinition> codeStashToAvalonEditHighlightLookups =
            new Dictionary<string, IHighlightingDefinition>();


        private AvalonEditHelper()
        {
            codeStashToAvalonEditTranslateLookups.Add("C","C++");
            codeStashToAvalonEditTranslateLookups.Add("C++","C++");
            codeStashToAvalonEditTranslateLookups.Add("C#","C#");
            codeStashToAvalonEditTranslateLookups.Add("CSS","CSS");
            codeStashToAvalonEditTranslateLookups.Add("Flex","");
            codeStashToAvalonEditTranslateLookups.Add("HTML","HTML");
            codeStashToAvalonEditTranslateLookups.Add("Java","Java");
            codeStashToAvalonEditTranslateLookups.Add("JavaScript", "JavaScript");
            codeStashToAvalonEditTranslateLookups.Add("JavaScript with DOM","");
            codeStashToAvalonEditTranslateLookups.Add("Perl","");
            codeStashToAvalonEditTranslateLookups.Add("PHP","PHP");
            codeStashToAvalonEditTranslateLookups.Add("Python","");
            codeStashToAvalonEditTranslateLookups.Add("Ruby","");
            codeStashToAvalonEditTranslateLookups.Add("SQL","");
            codeStashToAvalonEditTranslateLookups.Add("VB.NET", "VBNET");
            codeStashToAvalonEditTranslateLookups.Add("VB", "VBNET");
            codeStashToAvalonEditTranslateLookups.Add("XML","XmlDoc");


            AddHighlightDefinition("XmlDoc");
            AddHighlightDefinition("C#");
            AddHighlightDefinition("JavaScript");
            AddHighlightDefinition("HTML");
            AddHighlightDefinition("ASP/XHTML");
            AddHighlightDefinition("Boo");
            AddHighlightDefinition("Coco");
            AddHighlightDefinition("CSS");
            AddHighlightDefinition("C++");
            AddHighlightDefinition("Java");
            AddHighlightDefinition("Patch");
            AddHighlightDefinition("PHP");
            AddHighlightDefinition("TeX");
            AddHighlightDefinition("VBNET");
            AddHighlightDefinition("XML");
        }

        static AvalonEditHelper()
        {
        }

        public static AvalonEditHelper Instance
        {
            get { return instance.Value; }
        }

        public IHighlightingDefinition GetHighlightForCodeStashLookup(string languageCode)
        {
            IHighlightingDefinition def = null;

            if (codeStashToAvalonEditTranslateLookups.ContainsKey(languageCode))
            {
                string avalonLookup = codeStashToAvalonEditTranslateLookups[languageCode];
                if (!string.IsNullOrEmpty(avalonLookup))
                {
                    if (codeStashToAvalonEditHighlightLookups.ContainsKey(avalonLookup))
                    {
                        def = codeStashToAvalonEditHighlightLookups[avalonLookup];
                    }
                }
            }

            return def;
        }


        
        private void AddHighlightDefinition(string avalonLookup)
        {
            IHighlightingDefinition highlight = HighlightingManager.Instance.HighlightingDefinitions.Where(x => x.Name == avalonLookup).Single();
            if (highlight != null)
                codeStashToAvalonEditHighlightLookups.Add(avalonLookup, highlight);
        }

    }
}
